package com.zqh.messagemq.service;


import com.zqh.messagemq.config.RabbitMqConfig;
import com.zqh.myfmmall.clients.OrderCheckExpiredClient;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Description :
 * @Author :kono淇杭哒、
 * @Create 2021-12-09 22:26
 */
@Component
public class Consumer{
    public static final String DELAYED_QUEUE_NAME = "delayed.queue";
    public static final String DELAYED_EXCHANGE_NAME = "delayed.exchange";
    public static final String DELAYED_ROUTING_KEY = "delayed.routingkey";

    @Autowired
    private OrderCheckExpiredClient orderCheckExpiredClient;

    /**
     * 监听死信队列
     */
    /*@RabbitListener(queues = RabbitMqConfig.DELAYED_QUEUE_NAME)
    public void listenDelayedQueueMessage(String msg) throws InterruptedException {
        //若监听到的消息为订单消息
        if (msg.startsWith("order:")) {
            int index = msg.indexOf(":");
            //取出订单号
            String orderId = msg.substring(index + 1);
            orderCheckExpiredClient.checkExpiredOrder(orderId);
        }
    }*/

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = DELAYED_QUEUE_NAME,durable = "true"),
            exchange = @Exchange(name = DELAYED_EXCHANGE_NAME,delayed = "true"),
            key = DELAYED_ROUTING_KEY
    ))
    public void listenDelayedQueueMessage(String msg) {
        //若监听到的消息为订单消息
        if (msg.startsWith("order:")) {
            int index = msg.indexOf(":");
            //取出订单号
            String orderId = msg.substring(index + 1);
            orderCheckExpiredClient.checkExpiredOrder(orderId);
        }
        System.out.println(msg);
    }
}
